//
//  HotSearchView.m
//  WULIHH
//
//  Created by tang on 16/4/5.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "HotSearchView.h"
#import "HotSearchCollectionViewCell.h"

#import "WaterFlowLayout.h"

#import "WorkTableViewCell.h"
#import "SearchStarViewCell.h"

@interface HotSearchView ()<UITableViewDelegate, UITableViewDataSource, SearchStarViewCellDelegate>
{
    UITableView *mainTableView;
    
    NSArray *starArray;
    NSArray *workArray;
}

@end



@implementation HotSearchView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUserInteractionEnabled:YES];
        [self initUI];
    }
    return self;
}


- (void)initUI
{
    mainTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
    [self addSubview:mainTableView];
    [mainTableView setDataSource:self];
    [mainTableView setDelegate:self];
    
    
}


- (void)setInfoDic:(NSDictionary *)infoDic
{
//    NSLog(@"%@", infoDic);
    starArray = [[infoDic valueForKey:@"data"] valueForKey:@"star"];
    workArray = [[infoDic valueForKey:@"data"] valueForKey:@"work"];
    NSLog(@"%@", starArray);
    [mainTableView reloadData];
}
#pragma mark------


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (workArray.count == 0 || starArray.count == 0)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 0)
    {
//        return starArray.count;
        if (workArray.count == 0)
        {
            return starArray.count;
        }
        else if (starArray.count == 0)
        {
            return workArray.count;
        }
        else
        {
            return starArray.count;
        }
    }
    else
    {
        return workArray.count;
    }
    return 10;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellInden = @"cell";
    WorkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInden];

    NSString *cellInden2 = @"starcell";
    SearchStarViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellInden2];
    
    
    if (indexPath.section == 0)
    {
        if (workArray.count == 0)
        {
            //展示明星列表
            if (!cell2) {
                cell2 = [[SearchStarViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInden2];
            }
            [cell2 setModelwithName:[starArray[indexPath.row] valueForKey:@"name"] withImage:[starArray[indexPath.row] valueForKey:@"iconUrl"] buttonWithTag:(int)indexPath.row withFlow:[[starArray[indexPath.row] valueForKey:@"isFollowed"] intValue] withSectionInt:9999 withStarID:[[starArray[indexPath.row] valueForKey:@"id"] intValue]];
             [cell2 setMSearchStarViewCellDelegate:self];
            [cell2 setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell2;
        }
        else
        {
            //展示作品列表
            
            if (!cell) {
                cell = [[WorkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInden];
            }
            
            [cell.textLabel setText:@"1"];
            return cell;
        }
    }
    else
    {
        //展示作品列表
//        NSString *cellInden = @"cell";
//        WorkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInden];
        
        if (!cell) {
            cell = [[WorkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInden];
        }
        
        [cell.textLabel setText:@"1"];
        return cell;
    }
    
    
    
//    return cell;
}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        if (workArray.count == 0)
        {
            return @"明星";
        }
        else if (starArray.count == 0)
        {
            return @"作品";
        }
        else
        {
            return @"明星";
        }
    }
    else
    {
        return @"作品";
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (workArray.count == 0)
        {
            return 53.0;
        }
        else
        {
            
        }
    }
    else
    {
        return 53.0;
    }
    return 0;
}







//关注
- (void)pressGuanzhuWitghID:(NSString *)stringID withTag:(int)tag
{
//    NSLog(@"%@   %d", stringID, tag);
    
    NSLog(@"%@", starArray[tag]);
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
